package controllers.utils;

import java.net.URLDecoder;
import java.util.HashSet;
import java.util.Set;

import models.PropertyModel;


public class EntityUtil
{
    public static Set<String> getShortDescription(Set<PropertyModel> properties)
    {
        Set<String> uris = new HashSet<String>();
        uris.add("http://dbpedia.org/property/name");
        uris.add("http://www.w3.org/2000/01/rdf-schema#label");
        uris.add("http://xmlns.com/foaf/0.1/name");
        uris.add("http://dbpedia.org/property/alternativeNames");
        uris.add("http://dbpedia.org/property/id");
        uris.add("http://dbpedia.org/property/commonName");
        uris.add("http://dbpedia.org/property/name");
        uris.add("http://xmlns.com/foaf/0.1/surname");
        uris.add("/type/object/name");
        uris.add("/symbols/name_source/namesakes");
        uris.add("/type/object/id");
        uris.add("/common/topic/alias");
        uris.add("/type/object/key");

        Set<String> language = new HashSet<String>();
        language.add("en");
        language.add("XX");

        return EntityUtil.getPropertyModel(uris, properties, language);
    }


    private static Set<String> getPropertyModel(Set<String> uris, Set<PropertyModel> properties, Set<String> language)
    {
        Set<String> result = new HashSet<String>();

        for(PropertyModel property : properties) {
            if(!uris.contains(property.getURI())) {
                continue;
            }

            for(String lang : property.getValues().keySet()) {
                if(language.contains(lang)) {
                    for(String value : property.getValues().get(lang)) {

                        if(property.getURI().equals("http://dbpedia.org/property/alternativeNames")) {
                            String[] split = value.split(",");
                            for(int i = 0; i < split.length; i++) {
                                result.add(split[i].trim());
                            }
                        } else {
                            result.add(value);
                        }
                    }
                }
            }
        }

        return result;
    }


    public static Set<String> getLocalNameSet(Set<String> values)
    {
        Set<String> result = new HashSet<>();
        for(String s : values) {
            if(s == null || s.length() < 1) {
                continue;
            }
            result.add(getLocalName(s, true));
        }
        return result;
    }


    public static String getLocalName(String value, boolean standardize)
    {
        String s = value.toString().replace("_", " ");

        try {
            s = URLDecoder.decode(s, "UTF-8");
        } catch(Exception e) {
            // do nothing
        }

        if(s.contains("^^")) {
            s = s.substring(0, s.lastIndexOf("^^"));
        } else if(s.contains("#")) {
            s = s.substring(s.lastIndexOf("#") + 1);
        } else if(s.contains("/")) {
            s = s.substring(s.lastIndexOf("/") + 1);
        }

        if(standardize) {
            s = EntityUtil.standardize(s);
        }

        return s.toString();
    }


    public static String standardize(String s)
    {
        String result = s.toLowerCase();

        result = result.replaceAll("_", " ");
        result = result.replaceAll("-", " ");
        result = result.replaceAll("[(]", "");
        result = result.replaceAll("[)]", "");

        return result.trim();
    }
}